/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.fee.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.fee.entity.Receivable;
import com.jeeplus.modules.fee.mapper.ReceivableMapper;
import com.jeeplus.modules.fee.entity.ReceivablesDetailed;
import com.jeeplus.modules.fee.mapper.ReceivablesDetailedMapper;

/**
 * 应收Service
 * @author 李超辉
 * @version 2018-02-01
 */
@Service
@Transactional(readOnly = true)
public class ReceivableService extends CrudService<ReceivableMapper, Receivable> {

	@Autowired
	private ReceivablesDetailedMapper receivablesDetailedMapper;
	
	public Receivable get(String id) {
		Receivable receivable = super.get(id);
		receivable.setReceivablesDetailedList(receivablesDetailedMapper.findList(new ReceivablesDetailed(receivable)));
		return receivable;
	}
	
	public List<Receivable> findList(Receivable receivable) {
		return super.findList(receivable);
	}
	
	public Page<Receivable> findPage(Page<Receivable> page, Receivable receivable) {
		return super.findPage(page, receivable);
	}
	
	@Transactional(readOnly = false)
	public void save(Receivable receivable) {
		super.save(receivable);
		for (ReceivablesDetailed receivablesDetailed : receivable.getReceivablesDetailedList()){
			if (receivablesDetailed.getId() == null){
				continue;
			}
			if (ReceivablesDetailed.DEL_FLAG_NORMAL.equals(receivablesDetailed.getDelFlag())){
				if (StringUtils.isBlank(receivablesDetailed.getId())){
					receivablesDetailed.setReceivables(receivable);
					receivablesDetailed.preInsert();
					receivablesDetailedMapper.insert(receivablesDetailed);
				}else{
					receivablesDetailed.preUpdate();
					receivablesDetailedMapper.update(receivablesDetailed);
				}
			}else{
				receivablesDetailedMapper.delete(receivablesDetailed);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Receivable receivable) {
		super.delete(receivable);
		receivablesDetailedMapper.delete(new ReceivablesDetailed(receivable.getId()));
	}
	
}